<template>
  <el-container>
    <!-- 头部区域 -->
    <el-header class="home_header">
      <div class="home_header_top">
        <img src="@/assets/logo.png" alt="" class="home_header_logo" />
        <span class="home_header_span">CC后台管理系统</span>
      </div>
      <el-button type="info" @click="logout" class="home_header_logout"
        >退出</el-button
      >
    </el-header>
    <el-container>
      <el-aside :width="isOpen ? '64px' : '200px'">
        <!-- 折叠 -->
        <div class="home_aside_div" @click="collapse">|||</div>
        <!-- 侧边菜单区域 -->
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          :collapse="isOpen"
          :collapse-transition="false"
          router
          :default-active="active"
        >
          <!-- 一级菜单 -->
          <el-submenu
            :index="oneMenu.id + ''"
            v-for="(oneMenu, index) in menus"
            :key="oneMenu.id"
          >
            <!-- 一级菜单模板 -->
            <template slot="title">
              <!-- icon图标 -->
              <i :class="iconObj[oneMenu.id]"></i>
              <span>{{ oneMenu.authName }}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item
              :index="'/'+twoMenu.path"
              v-for="(twoMenu, index) in oneMenu.children"
              :key="twoMenu.id"
              @click="saveNavState('/'+twoMenu.path)"
            >
              <template slot="title">
                <!-- icon图标 -->
                <i class="el-icon-menu"></i>
                <span>{{ twoMenu.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容区域 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  name: "",
  data() {
    return {
      // 存储左侧菜单数据
      menus: [],
      // 设置一级菜单动态的icon图标
      iconObj: {
        125: "el-icon-user-solid",
        103: "el-icon-s-platform",
        101: "el-icon-s-cooperation",
        102: "el-icon-s-claim",
        145: "el-icon-s-marketing",
      },
      // 侧边菜单区域是否折叠
      isOpen: false,
      // 被激活的链接地址
      active:'',
    };
  },
  methods: {
    // 点击退出，跳转到登录界面
    logout() {
      this.$router.push("/login");
    },
    // 向服务器请求左侧菜单数据
    async getMenuList() {
      let { data } = await this.$http.get("menus");
      if (data.meta.status !== 200)
        return this.$message.error("获取菜单数据失败");
      this.menus = data.data;
    },
    // 绑定菜单折叠
    collapse() {
      this.isOpen = !this.isOpen;
    },
    // 保存链接的激活状态
    saveNavState(active){
      sessionStorage.setItem('ACTIVE',active)
      this.active = active
    }
  },
  mounted() {
    // 请求侧边菜单数据
    this.getMenuList();
    // 设置高亮，刷新之后也存在
    this.active = sessionStorage.getItem('ACTIVE')
  },
};
</script>

<style lang="less" scoped>
.el-header {
  background-color: #373d41;
}
.el-aside {
  background-color: #333744;
  .el-menu {
    // 菜单右侧对齐
    border-right: none;
  }
}
.el-main {
  background-color: #eaedf1;
}
.el-container {
  height: 100%;
}

.home_header {
  display: flex;
  justify-content: space-between;
  align-items: center;

  .home_header_top {
    display: flex;
    align-items: center;

    .home_header_logo {
      height: 50px;
    }
    .home_header_span {
      font-size: 20px;
      color: #ffffff;
      padding: 15px;
    }
  }
}

.home_aside_div {
  background-color: #4a5064;
  text-align: center;
  color: #fff;
  letter-spacing: 3px;
  line-height: 25px;
  cursor: pointer;
  font: 10px;
}
</style>
